package cn.wgx.module.mind.websocket.config.interceptor;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;


public class ChatHandlerShareInterceptor implements HandshakeInterceptor {


    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                   WebSocketHandler wsHandler, Map<String, Object> attributes) {
        System.out.println("beforeHandshake");
        boolean isLogin = true;
//        String path = request.getURI().getPath();
//        if(path != null && path.contains(WebSocketStompConfig.PATH_LIVE)){
//            attributes.put("uri", path);
//            return true;
//        }
//        //request获取session判断是否登录
//        Session session = WebSocketTool.getShiroSession(request);
//        if(null != session){
//            Collection<Object> attributeKeys = session.getAttributeKeys();
//            //sessionAttribute复制，从http复制到websocket
//            for(Object key: attributeKeys){
//                Object attribute = session.getAttribute(key);
//                if(UserUtil.SESSION_USER.equals(key)){
//                    isLogin = true;
//                }
//                attributes.put(key.toString(), attribute);
//                //System.out.println(attribute);
//            }
//        }
        return isLogin;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
                               WebSocketHandler wsHandler, Exception ex) {

        System.out.println("afterHandshake");
    }

}
